<%@page contentType="text/html;charset=utf-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <jsp:include page="common.jsp"/>
    <style type="text/css">
        .cque-list {
            margin: 10px;
            display: flex;
            flex-direction: column;
        }

        .cque-list-nav {
            margin: 10px;
        }

        .cque-list-content {
            margin: 10px;
        }

        .cque-td {
            display: flex;
            justify-content: space-around;
            align-items: center;
        }
    </style>
</head>

<body>
<div class="cque-window">
    <div class="cque-nav">
        <div class="navbar-header" style="margin-left: 20px">
            <img alt="Brand" height="60px" src="${pageContext.request.contextPath}/assets/images/logo.png">
        </div>
        <h2 style="margin-left: 20px">商城后台管理系统</h2>
    </div>
    <div class="cque-content">
        <div class="cque-side">
            <div class="panel panel-info" style="margin: 0;height: 100%">
                <a href="/admin/category/list"><div class="panel-heading" style="background-color:#d9edf7;">系统菜单</div></a>

                <div class="panel-body" style="padding: 0">
                    <div class="list-group">
                        <a href="/admin/category/list" class="list-group-item" style="background-color:#d9edf7;" >商品类别管理</a>
                        <a href="/admin/product/list"  class="list-group-item">商品管理</a>
                        <a href="/admin/order/list" class="list-group-item">订单管理</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="cque-main">
            <div class="cque-list">
                <div class="cque-list-nav">
                    <button type="button" class="btn btn-default " data-toggle="modal"
                            data-target="#myModa2">添加
                    </button>
                </div>
                <div class="cque-list-content">
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>名称</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach items="${categoryList}" var="list">
                            <tr>
                                <td>${list.cid}</td>
                                <td>${list.cname}</td>
                                <td class="cque-td">
                                    <!-- Button trigger modal -->
                                    <button type="button" class="update btn btn-default " data-toggle="modal"
                                            data-target="#myModal" onclick="update('${list.cid}','${list.cname}')"> 修改
                                    </button>
                                    </button>
                                    <button class="btn btn-default" onclick="del('${list.cid}')" type="button">删除</button>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">修改商品类别</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal ">
                    <div class="form-group">
                        <label for="cid" class="col-sm-offset-1 col-sm-3 control-label">商品类别ID</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="cid" readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="cname" class="col-sm-offset-1 col-sm-3 control-label">商品类别名称</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="cname">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary"  onclick="save()">保存</button>
            </div>
        </div>
    </div>
</div>


<!-- 添加Modal -->
<div class="modal fade" id="myModa2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabe2">新增商品类别</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal ">
                    <div class="form-group">
                        <label for="cid2" class="col-sm-offset-1 col-sm-3 control-label">商品类别ID</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="cid2" placeholder="请输入商品类别ID">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="cname2" class="col-sm-offset-1 col-sm-3 control-label">商品类别名称</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="cname2" placeholder="请输入类别名称">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="add" class=" btn  btn-primary">保存</button>
            </div>
        </div>
    </div>
</div>


<script>

    $('#add').click(function () {
        var cid = $('#cid2').val();
        var cname = $('#cname2').val();
   /*     $.post('/admin/category/add',{cid:cid,cname:cname},function (result) {
            if(result.code===1){
                location.href='/admin/category/list'
            }else {
                alert('添加失败！请重试...')
                location.href='/admin/category/list'
            }
        })*/

   $.ajax({
       type:"post",
       dataType:"json",
       data:{cid:cid,cname:cname},
       url:"/admin/category/add",
       success:function (data) {
           alert(data.info)
           location.reload();
       },
       error:function () {
           alert("添加失败,请重试");
       }
   })
    })

    //修改界面
    function update(cid, cname) {
        $('#cid').val(cid);
        $('#cname').val(cname);
    }

    function del(cid) {
        if (confirm('是否确认删除?')) {
            $.post('/admin/category/del', {cid: cid}, function (result) {
                if (result.code === 1) {
                    location.reload();
                }

            })
        } else {
            return false;
            location.href = '//admin/category/list'
        }
    }


    function save() {
        var cid = $('#cid').val();
        var cname = $('#cname').val();
        console.log(cid, cname)

        $.post('/admin/category/updates', {cid: cid, cname: cname}, function (result) {
            if (result.code === 1) {
                location.href = '/admin/category/list'
            } else {
                alert('修改失败，请重试...')
                location.href = '/admin/category/list'
            }
        })

    }
</script>
</body>
</html>